package com.express.gateway.common.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;

/**
 * 获取相应路径的工具
 * 
 * @author flower
 *
 */
public class PathUtil {
	private static final Logger LOG = LogManager.getLogger(PathUtil.class);
	/**
	 * 判断是否在jar环境中运行
	 * 
	 * @return 是返回true
	 */
	public static boolean isJarEnv() {
		System.out.println("is Jar Env:" + PathUtil.class.getResource(""));
		return Objects.requireNonNull(PathUtil.class.getResource("")).getPath().contains(".jar!");
	}

	/**
	 * 判断是否在jar环境中运行
	 * 
	 * @return 是返回true
	 */
	public static boolean isJarEnv(String fileName) {
		return Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(fileName)).getPath().contains(".jar!");
	}

	/**
	 * 获得根目录如果在jar中运行获得相对路径,反则返回当前线程运行的根目录
	 * 此方法只适用与 服务启动后  vertx.fileSystem()访问
	 * @param name
	 * @return
	 */
	public static String getPathString(String fileName) {
		if (fileName == null) {
			throw new NullPointerException("文件名字不能为空");
		}
		URL path = Thread.currentThread().getContextClassLoader().getResource(fileName);
		String url = Optional.ofNullable(path).map(URL::getPath).orElse(Strings.EMPTY);
		LOG.info("file uri: {}", path);
		if (Strings.isEmpty(url)) {
			return Strings.EMPTY;
		}
		if (url.contains(".jar!")) {
			return fileName;
		}
		return url;
	}

	public static String getFileContentString(String fileName) {
		if (fileName == null) {
			throw new NullPointerException("文件名字不能为空");
		}
		URL path = Thread.currentThread().getContextClassLoader().getResource(fileName);
		// path = PathUtil.class.getClassLoader().getResource(fileName);
		LOG.info("load file: {}", path);
		String url = Optional.ofNullable(path).map(URL::getPath).orElse(Strings.EMPTY);
		if (Strings.isEmpty(url)) {
			return Strings.EMPTY;
		}

		InputStream inStream = PathUtil.class.getClassLoader().getResourceAsStream(fileName);
		if (Objects.isNull(inStream)) {
			LOG.warn("can't find {} file.", fileName);
			return Strings.EMPTY;
		}
		LOG.info("inStream: {}", inStream);
		try (
			BufferedReader br = new BufferedReader(new InputStreamReader(inStream));) {
			String line = "";
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				// System.out.println(line);
				sb.append(line.trim().replaceAll("\t", Strings.EMPTY).replaceAll("\n", Strings.EMPTY));
			}
			LOG.debug("0-file content: {}", sb);
			return sb.toString();
		} catch (Exception e) {
			LOG.error("read file error:{}", e.getMessage(), e);
		}
		return Strings.EMPTY;
	}

	/**
	 * 通过名字获得项目的Path文件
	 * 
	 * @param fileName
	 * @return
	 */
	public static Path getPath(String fileName) {
		File file = new File(PathUtil.getPathString(fileName));

		return file.toPath();
	}

	/**
	 * 获得资源的流
	 * 
	 * @param fileName
	 * @return
	 */
	public static InputStream getStream(String fileName) {
		if (fileName == null) {
			throw new NullPointerException("文件名字不能为空");
		}
		return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
	}

}
